package com.iafme.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iafme.dto.req.MemberMessageReqDTO;
import com.iafme.dto.req.MessageReqDTO;
import com.iafme.dto.res.MessageResDTO;
import com.iafme.mapper.MessageMapper;
import com.iafme.model.MemberMessage;
import com.iafme.model.Message;
import com.iafme.service.IMemberService;
import com.iafme.service.MemberMessageService;
import com.iafme.service.MessageService;
import com.systemframework.constants.Constants;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/2/19 09:44
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Override
    public Page<MessageResDTO> lists(MessageReqDTO messageReqDTO) {
        Page<Message> page=new Page<>(messageReqDTO.getPageNum(), messageReqDTO.getPageSize());
        LambdaQueryWrapper<Message> like = Wrappers.lambdaQuery(Message.class)
                .like(StrUtil.isNotEmpty(messageReqDTO.getName()), Message::getName, messageReqDTO.getName())
                .orderByDesc(Message::getCreateTime);
        return Convert.convert(new TypeReference<Page<MessageResDTO>>() {},this.page(page,like));
    }

    private final IMemberService memberService;

    private final MemberMessageService memberMessageService;


    @Override
    public void saveMessage(MessageReqDTO messageReqDTO) {
        Message message = Convert.convert(Message.class, messageReqDTO);
        this.save(message);
        saveBatchMemberMessage(messageReqDTO,message.getId());
    }

    private void saveBatchMemberMessage(MessageReqDTO messageReqDTO,Integer messageId){
        List<MemberMessage> lists= ListUtil.list(true);
        List<Integer> memberIds =ListUtil.list(true);
        if(messageReqDTO.getSelectType().equals(Constants.ZERO)){
            memberIds = memberService.findMemberIds();
        }else{
            memberIds=messageReqDTO.getMemberIds();
        }
        if(CollUtil.isNotEmpty(memberIds) && memberIds.size()>0){
            memberIds.forEach(memberId->{
                MemberMessage memberMessage = new MemberMessage();
                memberMessage.setMessageId(messageId);
                memberMessage.setMemberId(memberId);
                lists.add(memberMessage);
            });
            memberMessageService.saveBatch(lists);
        }
    }

    @Override
    public void updateMessage(MessageReqDTO messageReqDTO) {
        Message message = Convert.convert(Message.class, messageReqDTO);
        this.updateById(message);
        LambdaQueryWrapper<MemberMessage> eq = Wrappers.lambdaQuery(MemberMessage.class).eq(MemberMessage::getMessageId, message.getId());
        memberMessageService.remove(eq);
        saveBatchMemberMessage(messageReqDTO,message.getId());
    }

    @Override
    public MessageResDTO findById(Integer id) {
        Message message = this.getById(id);
        MessageResDTO messageResDTO = Convert.convert(MessageResDTO.class, message);
        LambdaQueryWrapper<MemberMessage> eq = Wrappers.lambdaQuery(MemberMessage.class).eq(MemberMessage::getMessageId, message.getId());
        List<Integer> memberIds = memberMessageService.list(eq).stream().map(MemberMessage::getMemberId).collect(Collectors.toList());
        messageResDTO.setMemberIds(memberIds);
        List<Integer> memberIds1 = memberService.findMemberIds();
        if(memberIds.equals(memberIds1)){
            messageResDTO.setSelectType(Constants.ZERO);
        }else{
            messageResDTO.setSelectType(Constants.ONE);
        }
        return messageResDTO;
    }

    @Override
    public void memberMessageRead(List<MemberMessageReqDTO> reqDTOList) {
        List<MemberMessage> list=ListUtil.list(true);
        reqDTOList.forEach(reqDTO->{
            MemberMessage memberMessage=new MemberMessage();
            memberMessage.setId(reqDTO.getId());
            memberMessage.setStatus(Constants.ONE);
            list.add(memberMessage);
        });
        memberMessageService.updateBatchById(list);
    }

    @Override
    public void memberMessageDelete(List<MemberMessageReqDTO> reqDTOList) {
        List<Integer> ids = reqDTOList.stream().map(MemberMessageReqDTO::getId).collect(Collectors.toList());
        memberMessageService.removeByIds(ids);
    }
}
